package com.google.gwt.it2tool.server;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.TreeMap;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.users.User;
import com.google.gwt.it2tool.client.CustomFactoryOrder;
import com.google.gwt.it2tool.client.Factory;

@PersistenceCapable
public class CustomFactoryOrderJDO implements Serializable{
	
	@PrimaryKey
	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	private Key key;	
	
	@Persistent
	private User user;
	
	//Order number/ID
	@Persistent
	private ArrayList<String> ids = new ArrayList<String>();
	
	@Persistent
	private ArrayList<Integer> pos = new ArrayList<Integer>();
	
	public CustomFactoryOrderJDO(){
		super();
	}
	
	public CustomFactoryOrderJDO(TreeMap<String,Integer> order){
		super();
		setOrder(order);
	}
	
		
	public ArrayList<String> getIds() {
		return ids;
	}

	public void setIds(ArrayList<String> ids) {
		this.ids = ids;
	}

	public ArrayList<Integer> getPos() {
		return pos;
	}

	public void setPos(ArrayList<Integer> pos) {
		this.pos = pos;
	}

	public TreeMap<String, Integer> getOrder() {
		TreeMap<String, Integer> order = new TreeMap<String, Integer>();
		int size = ids.size();
		for(int i=0;i<size;i++){
			order.put(ids.get(i),pos.get(i));
		}
		return order;
	}

	public void setOrder(TreeMap<String, Integer> order) {
		for(String s: order.keySet()){
			if(!ids.contains(s)){
				ids.add(s);
				pos.add(order.get(s));
			}else{
				pos.set(ids.indexOf(s), order.get(s));
			}
		}
	}

	
	public Key getKey() {
		return key;
	}

	public void setKey(Key key) {
		this.key = key;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	
	public void set(CustomFactoryOrder order){
		setOrder(order.getOrder());
	}

}
